package com.lovo.csc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.gson.Gson;
import com.lovo.commom.WebRemote.IWebProductRemote;
import com.lovo.commom.sysRemote.ISysPromotionalSchemesRemote;
import com.lovo.commom.util.ConstantUtil;
import com.lovo.commom.vo.ResultVO;
import com.lovo.csc.bean.PromotionalSchemesBean;
import com.lovo.csc.service.IPromotionalSchemesService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

@RestController
@RequestMapping("promotional")
public class PromotionalSchemesController {

    @Autowired
    private IPromotionalSchemesService promotionalSchemesService;

    @Autowired
    private ISysPromotionalSchemesRemote sysPromotionalSchemesRemote;
    /**
     * 前端远程接口
     */
    @Autowired
    private IWebProductRemote webProductRemote;

    /**
     * 从队列中拉取促销信息
     * 拉取到之后先添加促销订单表
     * 在添加促销订单和商品订单的中间表
     * @param json
     */
    @RabbitListener(queues = "queuePromotional")
    public void getPromotional(String json) {
        PromotionalSchemesBean promotionalSchemesBean = JSONObject.parseObject(json, PromotionalSchemesBean.class);
        //添加订单
        promotionalSchemesService.addPromotional(promotionalSchemesBean);
        //远程调用前端接口，把商品名和折扣传过去
//        webProductRemote.updateIsPermission(promotionalSchemesBean.getProductName(),promotionalSchemesBean.getRate());
        ConstantUtil.WS_MSG.add("促销审核");
    }

    /**
     * 分页查询所有状态未0(未结束)的促销订单信息
     * @param pageNum 页码
     * @return 促销订单的分页对象集合
     */
    @RequestMapping("findByPage")
    public IPage<PromotionalSchemesBean> findByPage(Integer pageNum){
        return promotionalSchemesService.findByPage(pageNum);
    }

    /**
     * 审核通过，审核不通过
     * @param promotionalId
     * @param audit
     * @return
     */
    @RequestMapping("update")
    public ResultVO update(long promotionalId,int audit){
        ResultVO rs = new ResultVO();
        if (promotionalId != 0L){
            //根据订单id修改审核状态
            promotionalSchemesService.update(promotionalId, audit);
            PromotionalSchemesBean byId = promotionalSchemesService.findById(promotionalId);
            //调用远程接口，修改后端状态
            sysPromotionalSchemesRemote.updateStatus(byId.getNumber(), audit);
            rs.setJson("修改成功");
            rs.setTag(0);
        }else {
            rs.setJson("修改失败");
            rs.setTag(1);
        }
        return rs;
    }
}
